package A1_贪心算法;

public class LC_968监控二叉树 {
    public class TreeNode{
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(){}
        TreeNode(int val){
            this.val=val;
        }
        TreeNode(int val,TreeNode left,TreeNode right){
            this.val=val;
            this.left=left;
            this.right=right;
        }
    }
    int result=0;
    public int minCameraCover(TreeNode root) {
        if(traversal(root)==0)
            result++;
//        traversal(root);
        return result;
    }
    public int traversal(TreeNode cur){
        /*
        0:无覆盖
        1:有摄像头
        2:有覆盖

        1)左右都有覆盖
        2)左右至少有一个无覆盖
        3)左右至少有一个有摄像头
        4)根无覆盖，直接放摄像头
         */
        if(cur==null)
            return 2;
        int left=traversal(cur.left);
        int right=traversal(cur.right);
        //中
        if(left==2 && right==2)
            return 0;
        if(left==0 || right==0){
            result++;
            return 1;
        }
        if(left==1 || right==1)
            return 2;
        return -1;
    }
}
